#include "precompiled.h"
#include "qmlcontrol.h"
#include <QtDeclarative>
#include "helper.h"

class QmlControlPrivate{
	public:
	QmlControlPrivate():object(0)
	,view(0)
	{}
		~QmlControlPrivate()
		{
			delete view;
		}
		QObject* object;
		QDeclarativeView* view;
};

QmlControl::QmlControl(QmlControl *parent) :
	Control(parent), O_PRIVATE_CREATE(QmlControl)
{
	O_D(QmlControl);
	d->view = new QDeclarativeView(this);
	d->view->setGeometry(geometry());
}

QmlControl::~QmlControl()
{
	O_PRIVATE_DELETE;
}

QDeclarativeView* QmlControl::view()const
{
	O_C(QmlControl);
	return d->view;
}

QObject* QmlControl::object()const
{
	O_C(QmlControl);
	return d->object;
}

void QmlControl::hideEvent ( QHideEvent * event )
{
	O_D(QmlControl);
	d->view->hide();
}

void QmlControl::showEvent ( QShowEvent * event )
{
	O_D(QmlControl);
	d->view->show();
}

bool QmlControl::load(const QString& className, const QUrl& url)
{
	O_D(QmlControl);

	d->view->setSource(url);
	if (d->view->status() == QDeclarativeView::Error){
		LI.Error() << "Error while loading :" << className;
		return false;
	}
	d->object = d->view->rootObject();

//	for (int i = 0; i < d->object->children().size(); i++){
//		QObject * c = d->object->children().at(i);
//		QString prop =  c->metaObject()->className();
//		LI.Trace() << "--- " << prop << " ---";

//		for(int j = 0; j < c->metaObject()->propertyCount(); j++)
//		{
//			QMetaProperty p = c->metaObject()->property(j);
//			LI.Trace() << p.name() << "=" << c->property(p.name()).toString();
//		}

//		LI.Trace() << "=== " << prop << " ===";
//	}

	return true;
}
